package StudyCode.summary.zipfile;

import java.io.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreateBufferedInputStream {
	public static void main(String[] arguments) {
		boolean success = writeStream();
		System.out.println("\nReading: ");
		boolean readSuccess = readStream();
		System.out.println("从文件创建缓冲输入流并重新读取字节是否成功?" + readSuccess);
	}

	static boolean writeStream() {
		try (FileOutputStream file = new FileOutputStream("numbers.dat");
		     BufferedOutputStream buff = new BufferedOutputStream(file)) {

			for (int out = 0; out <= 200; out++) {
				buff.write(out);
				System.out.print(" " + out);
			}
			buff.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	static boolean readStream() {
		try (FileInputStream file = new FileInputStream("numbers.dat");
		     BufferedInputStream buff = new BufferedInputStream(file)) {
			int in;
			do {
				in = buff.read();
				if (in != -1)
					System.out.print(" " + in);
			} while (in != -1);
			buff.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
}
